package com.wpen.unit.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.system.DictionaryCvt;
import com.wpen.unit.exception.BizException;
import com.wpen.unit.infra.mybatis.system.po.DictionaryPo;
import com.wpen.unit.infra.repository.system.DictionaryRepository;
import com.wpen.unit.service.system.DictionaryService;
import com.wpen.unit.web.dto.system.DictionaryAddRequest;
import com.wpen.unit.web.dto.system.DictionaryAddResponse;
import com.wpen.unit.web.dto.system.DictionaryListRequest;
import com.wpen.unit.web.dto.system.DictionaryListResponse;
import com.wpen.unit.web.dto.system.DictionaryListRowsResponse;
import com.wpen.unit.web.dto.system.DictionaryModRequest;
import com.wpen.unit.web.dto.system.DictionaryModResponse;

/**
 * 系统数据字典业务处理.
 * 
 * @author Wang Peng
 * @date 2022-12-23
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Override
    public DictionaryListResponse queryListDictionary(DictionaryListRequest dictionaryListRequest){
        int index = dictionaryListRequest.getIndex();
    	int size = dictionaryListRequest.getSize();
        // 数据库查询
        DictionaryPo dictionaryPo = DictionaryCvt.INSTANCE.queryListDto2Do(dictionaryListRequest);

        long total = dictionaryRepository.count(dictionaryPo);
        List<DictionaryPo> dictionaryPoLists = dictionaryRepository.queryList(index, size,dictionaryPo);

        // 返回响应信息
        DictionaryListResponse dictionaryListResponse = new DictionaryListResponse();
        dictionaryListResponse.setTotal(total);
		dictionaryListResponse.setCount(0);
        if (dictionaryPoLists != null) {
            dictionaryListResponse.setCount(dictionaryPoLists.size());
            List<DictionaryListRowsResponse> listRows = dictionaryListResponse.getRows();
            for (DictionaryPo inDictionaryPo : dictionaryPoLists) {
                DictionaryListRowsResponse row = DictionaryCvt.INSTANCE.queryListRowDo2Dto(inDictionaryPo);
                // row.setId(inDictionaryPo.getId());
                // row.setDictGroup(inDictionaryPo.getDictGroup());
                // row.setDictLabel(inDictionaryPo.getDictLabel());
                // row.setDictValue(inDictionaryPo.getDictValue());
                // row.setDictStyle(inDictionaryPo.getDictStyle());
                // row.setDictDefault(inDictionaryPo.getDictDefault());
                // row.setOrders(inDictionaryPo.getOrders());
                // row.setDictPid(inDictionaryPo.getDictPid());
                // row.setDictDesc(inDictionaryPo.getDictDesc());
                // row.setStatus(inDictionaryPo.getStatus());
                // row.setVersion(inDictionaryPo.getVersion());
                // row.setUpdateTime(inDictionaryPo.getUpdateTime());
                listRows.add(row);
            }
            dictionaryListResponse.setRows(listRows);
        }

        return dictionaryListResponse;
    }


    @Override
    public DictionaryAddResponse addDictionary(DictionaryAddRequest dictionaryAddRequest ) {
        // 持久化操作
        DictionaryPo dictionaryPo = DictionaryCvt.INSTANCE.addDto2Do(dictionaryAddRequest);
        dictionaryPo.setVersion(1);
        //dictionaryPo.setUpdateTime(new Date());
        //保存数据
        dictionaryRepository.insert(dictionaryPo);

        // 返回响应信息
        DictionaryAddResponse dictionaryAddResponse = new DictionaryAddResponse();
        return dictionaryAddResponse;
    }



    @Override
    public DictionaryModResponse modDictionary(DictionaryModRequest dictionaryModRequest ){
        // 持久化操作
        DictionaryPo dictionaryPo = DictionaryCvt.INSTANCE.modDto2Do(dictionaryModRequest);
	    // 查询数据是否存在
        DictionaryPo dictionaryPoDb = dictionaryRepository.queryById(dictionaryPo.getId());
        if(dictionaryPoDb == null) {
            throw new BizException("E00001", dictionaryModRequest.getId() + "");
        }
        //更新数据
        // TODO change you need *******mod******** ↘↘↘requirement↙↙↙
        dictionaryPo.setId(dictionaryPoDb.getId());
        // dictionaryPo.setDictGroup(dictionaryPoDb.getDictGroup());
        // dictionaryPo.setDictLabel(dictionaryPoDb.getDictLabel());
        // dictionaryPo.setDictValue(dictionaryPoDb.getDictValue());
        // dictionaryPo.setDictStyle(dictionaryPoDb.getDictStyle());
        // dictionaryPo.setDictDefault(dictionaryPoDb.getDictDefault());
        // dictionaryPo.setOrders(dictionaryPoDb.getOrders());
        // dictionaryPo.setDictPid(dictionaryPoDb.getDictPid());
        // dictionaryPo.setDictDesc(dictionaryPoDb.getDictDesc());
        // dictionaryPo.setStatus(dictionaryPoDb.getStatus());
        dictionaryPo.setVersion(dictionaryPoDb.getVersion());
        // dictionaryPo.setUpdateTime(dictionaryPoDb.getUpdateTime());
        dictionaryRepository.updateById(dictionaryPo);

        // 返回响应信息
        DictionaryModResponse dictionaryModResponse = new DictionaryModResponse();
        return dictionaryModResponse;
    }


}
